#pragma once

#include <ClanLib/core.h>
#include <ClanLib/network.h>

namespace Game { class GameManager; }

namespace Network
{
	class ServerUser;
	class Server : public CL_Service //make server a Service/Daemon
	{
	public:
		Server();
		~Server();

		virtual void service_start(std::vector<CL_String> &args);
		virtual void service_stop();
		virtual void service_reload() {};

		void stopGame() { game_running = false; }
		void startGame() { game_running = true; }
		bool isGameRunning() { return game_running; }

		CL_NetGameServer &getNetGame() { return *net_game; }

	private:
		void exec();

		void onClientConnected(CL_NetGameConnection *connection);
		void onClientDisconnected(CL_NetGameConnection *connection);
		
		void onEventReceived(CL_NetGameConnection *connection, const CL_NetGameEvent &e);

		void onEventLogin(const CL_NetGameEvent &e, ServerUser *user);

		CL_Thread thread;
		CL_Event stop_event;
		
		CL_NetGameServer *net_game;
		CL_SlotContainer slots;
		CL_String port;

		CL_NetGameEventDispatcher_v1<ServerUser*> login_events;
//		CL_NetGameEventDispatcher_v1<ServerUser*> game_events;

		int next_user_id;
		bool game_running;
		Game::GameManager *gameMgr;
	};
}
